<?php 
class SalesPortalLoginCheckerUtil{
	public static function canUserAccessCustomer($model = null){
		if( $model!=null ){
			$customer = null;
			if($model instanceof CActiveRecord && isset($model->custno) && Yii::app()->user->isGuest == false){
				$customer = Customers::model()->findByPk($model->custno);
				
			}elseif(is_string($model)){
				$customer = Customers::model()->findByPk($model);
			}
			if($customer!=null){
				if(Yii::app()->user->empno == $customer->salesperson){
					return true;
				}
				else{
					throw new CHttpException('401',"$customer->custno - $customer->lastname is assigned to a different sales person.  Please contact management if you believe this is an error.");				
				}
			}	
		}
		return false;
	}

}